﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;

using ISS.CaseStudy.BLL;
using ISS.CaseStudy.Entities;

namespace CaseStudy.Web.UserControl
{
    public partial class ComponentRelatedInfo : System.Web.UI.UserControl
    {
        public string RelatedTitle { get; set; }
        public RelatedType Type { get; set; }
        public RelatedZone Zone { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            int relatedInfoCount = 0;
            riTitle.InnerText = RelatedTitle;

            DataSet dataSet = RelatedInformationBLL.GetAll();
            if (dataSet.Tables.Count == 0) return;

            if (Type.ToString().ToLower().Equals("category"))
            {
                foreach (DataRow row in dataSet.Tables[0].Rows)
                {
                    if ((Zone == RelatedZone.None) ||
                        row[Zone.ToString()].ToString().Equals("0"))
                        continue;

                    if (row["IsGlobal"].ToString().Equals("1"))
                    {
                        HyperLink link = new HyperLink();
                        link.Text = row["Title"].ToString();
                        link.NavigateUrl = row["Url"].ToString();
                        link.CssClass = "RelateInfoItem";

                        riContent.Controls.Add(link);
                        relatedInfoCount++;
                    }
                    else
                    {
                        if (Request.QueryString["type"] == null)
                            continue;

                        string category = Request.QueryString["type"];
                        if (row["Category"].ToString().Contains(category))
                        {
                            HyperLink link = new HyperLink();
                            link.Text = row["Title"].ToString();
                            link.NavigateUrl = row["Url"].ToString();
                            link.CssClass = "RelateInfoItem";

                            riContent.Controls.Add(link);
                            relatedInfoCount++;
                        }
                    }
                }
            }
            else
            {
                List<RelatedInfo> list = new List<RelatedInfo>();

                foreach (DataRow row in dataSet.Tables[0].Rows)
                {
                    if ((Zone == RelatedZone.None) ||
                        row[Zone.ToString()].ToString().Equals("0"))
                        continue;

                    RelatedInfo relatedInfo = new RelatedInfo();
                    relatedInfo.Id = row["Id"].ToString();
                    relatedInfo.Title = row["Title"].ToString();
                    relatedInfo.Url = row["Url"].ToString();
                    relatedInfo.IsGlobal = row["IsGlobal"].ToString().Equals("1") ? true : false;

                    list.Add(relatedInfo);
                }

                if (Request.QueryString["id"] == null)
                {
                    this.Visible = false;
                    return;
                }

                string caseStudyId = Request.QueryString["id"];
                int result;
                if (!int.TryParse(caseStudyId, out result))
                {
                    this.Visible = false;
                    return;
                }

                List<string> relatedIds = CaseStudyRelatedInformationBLL.GetInfoIdsByCaseStudyId(caseStudyId);

                foreach (RelatedInfo relatedInfo in list)
                {
                    if ((relatedIds.Contains(relatedInfo.Id)) ||
                        relatedInfo.IsGlobal)
                    {
                        HyperLink link = new HyperLink();
                        link.Text = relatedInfo.Title.ToString();
                        link.NavigateUrl = relatedInfo.Url.ToString();
                        link.CssClass = "RelateInfoItem";

                        riContent.Controls.Add(link);
                        relatedInfoCount++;
                    }
                }
            }

            if (relatedInfoCount == 0)
                this.Visible = false;
        }
    }
}